package com.ys.controller.web.storage;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.entity.storage.TccWarehouseArea;
import com.ys.service.sys.CommonApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/web/storage/tccWarehouseArea")
public class WebTccWarehouseAreaController {

	@Autowired
	private CommonApiService<TccWarehouseArea> commonApiService;

	Long menuId = MenuId.TCC_WarehouseArea;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		try {
			TccWarehouseArea entity = new TccWarehouseArea();
			entity.setCkq(commonApiService.getCommonNextUkey(MenuId.TCC_WarehouseArea, "ckq"));
			entity.setStatus(1);
			return commonApiService.toAdd(model, entity, menuId, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
						 Long id){
		if (id == null){
			return W.error(model, "id不能为空");
		}

		if (new TccWarehouseArea().setId(id).count() == 0){
			return W.error(model, "该信息不存在!");
		}

		try {
			Map<String, Object> extraParam = getExtraParam(id);
			return commonApiService.toEdit(model, id, new TccWarehouseArea(), MenuId.TCC_WarehouseArea, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}


	}
	public Map<String,Object> getExtraParam(Long id){
		Map<String,Object> extraParam = new TccWarehouseArea()
				.addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
				.addLeftJoin("TB_Country tb ON tb.code = B.countryCode")
				.addField("B.Vck vck,B.CountryCode countryCode,B.City city,tb.name countryName")
				.setId(id)
				.queryFirstMap();

		return extraParam;
	}

}
